adminSettings = (function(){
	var _id, upgradeTrigger;
	return{
		init: function(){
			jQuery('div.op_image_selection_img').click(adminSettings.onSelect);
			jQuery('a.module_upgrade').click(adminSettings.upgradeModule);
		},
		
		onSelect: function(e){
			var target = e.srcElement || e.currentTarget; // get click event
			
			_id = target.id.split('-')[1]; // image id's are structured as i.e. image-1
			_id = _id == 0 ? '' : _id; // if user has selected no image, leave field blank
			jQuery(':input#op_header_img').eq(0).val(_id); // set hidden input value to image id
			adminSettings.selectImage(); // highlight image
		},
		
		selectImage: function(){
			_id = _id == '' ? 0 : _id; // if user has selected no image, leave field blank
		
			// adjust image classes based on newly selected image	
			jQuery('div.op_image_selection_img').each(function(){
				if(this.id.split('-')[1] == _id){
					jQuery(this).children().eq(0).addClass('selected');
				}
				else{
					jQuery(this).children().eq(0).removeClass('selected');
				}
			});
		},
		
		upgradeModule: function(e){
			upgradeTrigger = e.srcElement || e.currentTarget; // get click event
			var module = upgradeTrigger.getAttribute('module');
			
			jQuery.ajax({
				type: 'post',
				url: 'admin-ajax.php',
				data: 'action=op_upgrademodule&module='+module,
				success: adminSettings.upgradeModuleSuccess
			});
			return false;
		},
		
		upgradeModuleSuccess: function(e){
			var parent = jQuery(upgradeTrigger).parent();
			parent.html(e);
			parent.css({backgroundColor: "#333"});
			setTimeout(function(){parent.animate({backgroundColor:"#666"},500)},2000);
		}
		
	}
})();

jQuery(document).ready(adminSettings.init);